﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2008-8-13 17:43:17
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 OPERATION
    /// </summary>
    public class OPERATION : IOPERATION
    {
        public OPERATION()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.OPERATION model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [OPERATION](");
            strSql.Append("[OP_CODE],[OP_NAME],[OP_FIELD1],[OP_FIELD2])");
            strSql.Append(" values (");
            strSql.Append("@OP_CODE,@OP_NAME,@OP_FIELD1,@OP_FIELD2)");
            SqlParameter[] parameters = {
                    new SqlParameter("@OP_CODE", SqlDbType.VarChar,15),
                    new SqlParameter("@OP_NAME", SqlDbType.VarChar,100),
                    new SqlParameter("@OP_FIELD1", SqlDbType.VarChar,20),
                    new SqlParameter("@OP_FIELD2", SqlDbType.VarChar,20)
            };
            parameters[0].Value = model.OP_CODE;
            parameters[1].Value = model.OP_NAME;

            if (model.OP_FIELD1 != null)
                parameters[2].Value = model.OP_FIELD1;
            else
                parameters[2].Value = DBNull.Value;


            if (model.OP_FIELD2 != null)
                parameters[3].Value = model.OP_FIELD2;
            else
                parameters[3].Value = DBNull.Value;


            if (SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0)
                   return model.OP_CODE;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.OPERATION model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [OPERATION] set ");
            strSql.Append("[OP_NAME]=@OP_NAME,");
            strSql.Append("[OP_STOP]=@OP_STOP,");
            strSql.Append("[OP_FIELD1]=@OP_FIELD1,");
            strSql.Append("[OP_FIELD2]=@OP_FIELD2");
            strSql.Append(" where [OP_CODE]=@OP_CODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@OP_CODE", SqlDbType.VarChar,15),
                    new SqlParameter("@OP_NAME", SqlDbType.VarChar,100),
                    new SqlParameter("@OP_STOP", SqlDbType.SmallInt),
                    new SqlParameter("@OP_FIELD1", SqlDbType.VarChar,20),
                    new SqlParameter("@OP_FIELD2", SqlDbType.VarChar,20)
            };
            parameters[0].Value = model.OP_CODE;
            parameters[1].Value = model.OP_NAME;
            parameters[2].Value = model.OP_STOP;

            if (model.OP_FIELD1 != null)
                parameters[3].Value = model.OP_FIELD1;
            else
                parameters[3].Value = DBNull.Value;


            if (model.OP_FIELD2 != null)
                parameters[4].Value = model.OP_FIELD2;
            else
                parameters[4].Value = DBNull.Value;


            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string OP_CODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [OPERATION] ");
            strSql.Append(" where [OP_CODE]=@OP_CODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@OP_CODE", SqlDbType.VarChar,15)};
            parameters[0].Value = OP_CODE;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string OP_CODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [OPERATION]");
            strSql.Append(" where [OP_CODE]= @OP_CODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@OP_CODE", SqlDbType.VarChar,15)};
            parameters[0].Value = OP_CODE;
            return SqlHelper.Exists(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.OPERATION GetModel(string OP_CODE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [OPERATION] ");
            strSql.Append(" where [OP_CODE]=@OP_CODE");
            SqlParameter[] parameters = {
                    new SqlParameter("@OP_CODE", SqlDbType.VarChar,15)};
            parameters[0].Value = OP_CODE;
            DataSet ds = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据集,建议只在多表联查时使用
        /// </summary>
        public DataSet GetDataSet(string strSql)
        {
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql);
        }

        /// <summary>
        /// 获取泛型数据列表,建议只在多表联查时使用
        /// </summary>
        public IList<Model.OPERATION> GetList(string strSql)
        {
            return GetList(GetDataSet(strSql));
        }

        /// <summary>
        /// 获取数据集,在单表查询时使用
        /// </summary>
        public DataSet GetDataSet(string strWhat, string strWhere, string strOrderby)
        {
            if (string.IsNullOrEmpty(strWhat))
                strWhat = "*";
            StringBuilder strSql = new StringBuilder("select " + strWhat + " from [OPERATION]");
            if (!string.IsNullOrEmpty(strWhere))
                strSql.Append(" where " + strWhere);
            if (!string.IsNullOrEmpty(strOrderby))
                strSql.Append(" order by " + strOrderby);
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString());
        }

        /// <summary>
        /// 获取泛型数据列表,在单表查询时使用
        /// </summary>
        public IList<Model.OPERATION> GetList(string strWhat, string strWhere, string strOrderby)
        {
            return GetList(GetDataSet(strWhat, strWhere, strOrderby));
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.OPERATION> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[OPERATION]", null, pageSize, pageIndex, fldSort, sort, strCondition, "OP_CODE", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.OPERATION GetModel(DataRow r)
        {
            Model.OPERATION model = new Model.OPERATION();
            model.OP_CODE = SqlHelper.GetString(r["OP_CODE"]);
            model.OP_NAME = SqlHelper.GetString(r["OP_NAME"]);
            model.OP_STOP = SqlHelper.GetInt(r["OP_STOP"]);
            model.OP_FIELD1 = SqlHelper.GetString(r["OP_FIELD1"]);
            model.OP_FIELD2 = SqlHelper.GetString(r["OP_FIELD2"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.OPERATION> GetList(DataSet ds)
        {
            List<Model.OPERATION> l = new List<Model.OPERATION>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
